﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain
{
	using System;
	using System.Collections.Generic;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain.Enums;

	public partial class Forum : EntityBase<Forum>
	{
		// private fields
		private ForumPost _lastPost;

		/// <summary>
		/// Initializes a new instance of the <see cref="Forum"/> class.
		/// </summary>
		public Forum()
		{
			DeterminePermission(Roles.Forums);
		}

		/// <summary>
		/// Gets or sets the parent id.
		/// </summary>
		/// <value>The parent id.</value>
		public virtual int ParentId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the last post id.
		/// </summary>
		/// <value>The topic id.</value>
		public virtual int LastPostId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public virtual string Title
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the slug.
		/// </summary>
		/// <value>The slug.</value>
		public virtual string Slug
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public virtual string Body
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the body parsed.
		/// </summary>
		/// <value>The body parsed.</value>
		public virtual string BodyParsed
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the password.
		/// </summary>
		/// <value>The password.</value>
		public virtual string Password
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the order.
		/// </summary>
		/// <value>The order.</value>
		public virtual int Order
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the depth.
		/// </summary>
		/// <value>The depth.</value>
		public virtual int Depth
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="Forum"/> is HTML.
		/// </summary>
		/// <value><c>true</c> if HTML; otherwise, <c>false</c>.</value>
		public virtual bool Html
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether [BB code].
		/// </summary>
		/// <value><c>true</c> if [BB code]; otherwise, <c>false</c>.</value>
		public virtual bool BBCode
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="Forum"/> is smilies.
		/// </summary>
		/// <value><c>true</c> if smilies; otherwise, <c>false</c>.</value>
		public virtual bool Smilies
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public virtual ForumScope Scope
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the status.
		/// </summary>
		/// <value>The status.</value>
		public virtual ForumStatus Status
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the view age.
		/// </summary>
		/// <value>The view age.</value>
		public virtual ForumViewAge ViewAge
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the created.
		/// </summary>
		/// <value>The date & time when created.</value>
		public virtual DateTime Created
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the updated.
		/// </summary>
		/// <value>The date & time when updated.</value>
		public virtual DateTime Updated
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the total.
		/// </summary>
		/// <value>The total.</value>
		public virtual ForumTotal Total
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the last post.
		/// </summary>
		/// <value>The last post.</value>
		public virtual ForumPost LastPost
		{
			get
			{
				return _lastPost;
			}
			set
			{
				_lastPost = value;

				if (_lastPost != null)
				{
					this.LastPostId = _lastPost.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the childern.
		/// </summary>
		/// <value>The childern.</value>
		public virtual IList<Forum> Childern
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the announcements.
		/// </summary>
		/// <value>The announcements.</value>
		public virtual IList<ForumAnnouncement> Announcements
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the topics.
		/// </summary>
		/// <value>The topics.</value>
		public virtual IPagedList<ForumTopic, ForumTopicSpecification> Topics
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the permissions.
		/// </summary>
		/// <value>The permissions.</value>
		public virtual IList<ForumPermission> Permissions
		{
			get; 
			set;
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<Forum> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}